package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.RightInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * RightInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.RightInfo
 * @author MyEclipse Persistence Tools
 */

public class RightInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(RightInfoDAO.class);
	// property constants
	public static final String RIGHT_NAME = "rightName";
	public static final String RIGHT_DESC = "rightDesc";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(RightInfo transientInstance) {
		log.debug("saving RightInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(RightInfo persistentInstance) {
		log.debug("deleting RightInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RightInfo findById(java.lang.String id) {
		log.debug("getting RightInfo instance with id: " + id);
		try {
			RightInfo instance = (RightInfo) getHibernateTemplate().get(
					"com.moody.hibernate.domain.RightInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(RightInfo instance) {
		log.debug("finding RightInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Object> findByProperty(String propertyName, Object value) {
		log.debug("finding RightInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from RightInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Object> findByRightName(Object rightName) {
		return findByProperty(RIGHT_NAME, rightName);
	}

	public List<Object> findByRightDesc(Object rightDesc) {
		return findByProperty(RIGHT_DESC, rightDesc);
	}

	public List<Object> findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List<Object> findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List<Object> findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List<Object> findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List<Object> findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List<Object> findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all RightInfo instances");
		try {
			String queryString = "from RightInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RightInfo merge(RightInfo detachedInstance) {
		log.debug("merging RightInfo instance");
		try {
			RightInfo result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RightInfo instance) {
		log.debug("attaching dirty RightInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(RightInfo instance) {
		log.debug("attaching clean RightInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RightInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RightInfoDAO) ctx.getBean("RightInfoDAO");
	}

	public void update(RightInfo role) {
		log.debug("attaching clean UserInfo instance");
		try {
			getHibernateTemplate().update(role);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<String> findAvailableUrlListByUserId(String userId){
		String queryString = "select ri.availableUrl from RightInfo as ri, RoleRight as rr, UserInfo as ui where ri.rightId = rr.rightId and rr.roleId = ui.roleId and ui.userId = ?";
		return getHibernateTemplate().find(queryString,userId);
		
	}
}